<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发票ID" prop="invoiceId">
        <el-input
          v-model="queryParams.invoiceId"
          placeholder="请输入发票ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceStatus">
        <el-select v-model="queryParams.invoiceStatus" placeholder="请选择发票状态" clearable>
          <el-option
            v-for="dict in dict.type.invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收件人电话" prop="recipientPhone">
        <el-input
          v-model="queryParams.recipientPhone"
          placeholder="请输入收件人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行程开始时间" prop="travelTimeStart">
        <el-date-picker clearable
          v-model="queryParams.travelTimeStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择行程开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="行程结束时间" prop="travelTimeEnd">
        <el-date-picker clearable
          v-model="queryParams.travelTimeEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择行程结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['platform:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['platform:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['platform:detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['platform:detail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发票详情ID" align="center" prop="detailId" />
      <el-table-column label="驳回原因" align="center" prop="rejectReason" />
      <el-table-column label="发票状态" align="center" prop="invoiceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invoice_status" :value="scope.row.invoiceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="发票抬头" align="center" prop="invoiceTitle" />
      <el-table-column label="纳税人识别号" align="center" prop="taxpayerId" />
      <el-table-column label="发票类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invoice_type" :value="scope.row.invoiceType"/>
        </template>
      </el-table-column>
      <el-table-column label="发票内容" align="center" prop="invoiceContent" />
      <el-table-column label="注册地址和电话" align="center" prop="registerAddressPhone" />
      <el-table-column label="开户行和账号" align="center" prop="bankAccount" />
      <el-table-column label="收件人" align="center" prop="recipient" />
      <el-table-column label="收件人电话" align="center" prop="recipientPhone" />
      <el-table-column label="收件人地址" align="center" prop="recipientAddress" />
      <el-table-column label="行程单接收邮箱" align="center" prop="travelReceiptEmail" />
      <el-table-column label="申请日期" align="center" prop="applyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行程开始时间" align="center" prop="travelTimeStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.travelTimeStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行程结束时间" align="center" prop="travelTimeEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.travelTimeEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行程人手机号" align="center" prop="travelPhone" />
      <el-table-column label="合计金额" align="center" prop="totalAmount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['platform:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发票ID" prop="invoiceId">
          <el-input v-model="form.invoiceId" placeholder="请输入发票ID" />
        </el-form-item>
        <el-form-item label="驳回原因" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入驳回原因" />
        </el-form-item>
        <el-form-item label="发票状态" prop="invoiceStatus">
          <el-select v-model="form.invoiceStatus" placeholder="请选择发票状态">
            <el-option
              v-for="dict in dict.type.invoice_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票抬头" prop="invoiceTitle">
          <el-input v-model="form.invoiceTitle" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="纳税人识别号" prop="taxpayerId">
          <el-input v-model="form.taxpayerId" placeholder="请输入纳税人识别号" />
        </el-form-item>
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select v-model="form.invoiceType" placeholder="请选择发票类型">
            <el-option
              v-for="dict in dict.type.invoice_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票内容">
          <editor v-model="form.invoiceContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="注册地址和电话" prop="registerAddressPhone">
          <el-input v-model="form.registerAddressPhone" placeholder="请输入注册地址和电话" />
        </el-form-item>
        <el-form-item label="开户行和账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入开户行和账号" />
        </el-form-item>
        <el-form-item label="收件人" prop="recipient">
          <el-input v-model="form.recipient" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="收件人电话" prop="recipientPhone">
          <el-input v-model="form.recipientPhone" placeholder="请输入收件人电话" />
        </el-form-item>
        <el-form-item label="收件人地址" prop="recipientAddress">
          <el-input v-model="form.recipientAddress" placeholder="请输入收件人地址" />
        </el-form-item>
        <el-form-item label="行程单接收邮箱" prop="travelReceiptEmail">
          <el-input v-model="form.travelReceiptEmail" placeholder="请输入行程单接收邮箱" />
        </el-form-item>
        <el-form-item label="申请日期" prop="applyDate">
          <el-date-picker clearable
            v-model="form.applyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="行程开始时间" prop="travelTimeStart">
          <el-date-picker clearable
            v-model="form.travelTimeStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择行程开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="行程结束时间" prop="travelTimeEnd">
          <el-date-picker clearable
            v-model="form.travelTimeEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择行程结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="行程人手机号" prop="travelPhone">
          <el-input v-model="form.travelPhone" placeholder="请输入行程人手机号" />
        </el-form-item>
        <el-form-item label="合计金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入合计金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/platform/detail"

export default {
  name: "Detail",
  dicts: ['invoice_status', 'invoice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票详情表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        invoiceId: null,
        invoiceStatus: null,
        recipientPhone: null,
        travelTimeStart: null,
        travelTimeEnd: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        invoiceId: [
          { required: true, message: "发票ID不能为空", trigger: "blur" }
        ],
        invoiceTitle: [
          { required: true, message: "发票抬头不能为空", trigger: "blur" }
        ],
        taxpayerId: [
          { required: true, message: "纳税人识别号不能为空", trigger: "blur" }
        ],
        invoiceContent: [
          { required: true, message: "发票内容不能为空", trigger: "blur" }
        ],
        registerAddressPhone: [
          { required: true, message: "注册地址和电话不能为空", trigger: "blur" }
        ],
        bankAccount: [
          { required: true, message: "开户行和账号不能为空", trigger: "blur" }
        ],
        recipient: [
          { required: true, message: "收件人不能为空", trigger: "blur" }
        ],
        recipientPhone: [
          { required: true, message: "收件人电话不能为空", trigger: "blur" }
        ],
        recipientAddress: [
          { required: true, message: "收件人地址不能为空", trigger: "blur" }
        ],
        applyDate: [
          { required: true, message: "申请日期不能为空", trigger: "blur" }
        ],
        travelTimeStart: [
          { required: true, message: "行程开始时间不能为空", trigger: "blur" }
        ],
        travelTimeEnd: [
          { required: true, message: "行程结束时间不能为空", trigger: "blur" }
        ],
        travelPhone: [
          { required: true, message: "行程人手机号不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "合计金额不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询发票详情列表 */
    getList() {
      this.loading = true
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        detailId: null,
        invoiceId: null,
        rejectReason: null,
        invoiceStatus: null,
        invoiceTitle: null,
        taxpayerId: null,
        invoiceType: null,
        invoiceContent: null,
        registerAddressPhone: null,
        bankAccount: null,
        recipient: null,
        recipientPhone: null,
        recipientAddress: null,
        travelReceiptEmail: null,
        applyDate: null,
        travelTimeStart: null,
        travelTimeEnd: null,
        travelPhone: null,
        totalAmount: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.detailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加发票详情"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const detailId = row.detailId || this.ids
      getDetail(detailId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改发票详情"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.detailId != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const detailIds = row.detailId || this.ids
      this.$modal.confirm('是否确认删除发票详情编号为"' + detailIds + '"的数据项？').then(function() {
        return delDetail(detailIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('platform/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
